package com.leetcode.algorithm.y22.m06;

/**
 * 566. 重塑矩阵
 * 
 * https://leetcode.cn/problems/reshape-the-matrix/
 * 
 * @author jie.deng
 *
 */
class Question0566Solution01 {

	public int[][] matrixReshape(int[][] mat, int r, int c) {
		int m = mat.length;
		int n = mat[0].length;
		if (m * n != r * c) {
			return mat;
		}

		int[][] ret = new int[r][c];
		for (int i = 0; i < r; i++) {
			for (int j = 0; j < c; j++) {
				int idx = i * c + j;
				ret[i][j] = mat[idx / n][idx % n];
			}
		}
		return ret;
	}

}